<?php
$this->AddHook('other_work', 'Factory');

function Factory($bot){
	//==========================================================================
	//First run
	if ($bot->firstrun){
		return;
	}
	//==========================================================================
	//Description
	$vername='Factory';
	$verauth='Anhoc';
	
	if(file_exists('Plugins\Factory\description.txt')){
		$verdesc=file('Plugins\Factory\description.txt');
	}
	
	foreach ($verdesc as $dsc) {
		$dkey='';$dval='';
		list($dkey,$dval)=explode("=",$dsc);
		if (isset($dkey))
		if ($dkey=="name") $vername=trim($dval);
		if ($dkey=="version") $version=trim($dval);
		if ($dkey=="date") $verdate=trim($dval);
		if ($dkey=="author") $verauth=trim($dval);
		if ($dkey=="descr") $verdescr=trim($dval);
	}
	

	$data = $bot->ld->GetPlSettings("Factory");
	if (!isset($data)){
		return;
	}
	
	//==========================================================================
	//Run plugin
	if(isset($data->run)){
		if($data->run == "1"){
		
			//==========================================================================
			//Starting
			$bot->SendMsg('=====================================');
			$msg  = $bot->irc('u').$bot->irc('b');
			$msg .= $bot->irc('cc').'2';
			$msg .= $bot->irc('ff').'13,5'.chr(232).$bot->irc('ff').'14,5';
			$msg .= 'Starting Plugin: '.$vername.' v'.$version;
			$bot->SendMsg($msg);
			
			//==========================================================================
			// Manage max premium goods
			$NoHarvestIfFull = 0;
			if (isset($data->NoHarvestIfFull)){
				if ($data->NoHarvestIfFull == "1"){
					$NoHarvestIfFull = 1;
				}
			}

			$StopHarvest = 0;
			if (($bot->PremiumGoodsMax * 0.9) <= $bot->PremiumGoods){
				if ($NoHarvestIfFull == 1){
					$StopHarvest = 1;
					$bot->SendMsg('[Factory] Premium Goods storage > 90% Full. Not Harvesting. '.$bot->PremiumGoods."/".$bot->PremiumGoodsMax);
				}
			}
			//==========================================================================
			// Start work
			$bot->SendMsg('=====================================');
			$bot->SendMsg($bot->irc('b').$bot->irc('cc').'5[Factory] Main World Factory');
			if ($bot->currentWorld != "world_main") $bot->LoadWorldObjects("world_main");
			if(count($data->collectFactoryIN) == 0){
				$bot->SendMsg('=====================================');
				$bot->SendMsg($bot->irc('cc').'4[Factory] You did not select any factory.');
			}
			
			if($bot->energy <= 0){
				$bot->CheckEnergy();//Check if has energy on iventory;
			}
		
			if($bot->energy > 0){
				//Harvest Factory
				if($StopHarvest == 0){
					HavestMainFactory($bot, $data);
				}
				//Place Contract
				PlaceContractOnMainFactory($bot, $data);
			}
			$bot->SendMsg('=====================================');
			$bot->SendMsg($bot->irc('b').$bot->irc('cc').'5[Factory] Downtown Factory');
			if ($bot->currentWorld != "world_downtown") $bot->LoadWorldObjects("world_downtown");
			if(count($data->collectFactoryINDt) == 0){
				$bot->SendMsg('=====================================');
				$bot->SendMsg($bot->irc('cc').'4[Factory] You did not select any factory.');
			}
			
			if($bot->energy <= 0){
				$bot->CheckEnergy();//Check if has energy on iventory;
			}
		
			if($bot->energy > 0){
				//Harvest Factory
				if($StopHarvest == 0){
					HavestDtFactory($bot, $data);
				}
				//Place Contract
				PlaceContractOnDtFactory($bot, $data);
			}
			//==========================================================================
			// Finishing
			$bot->SendMsg('=====================================');
			$bot->pm->RefreshMePlugin("Factory");
		}
	}
}
//==========================================================================
//Harvest Main Factory
function HavestMainFactory($bot, $data){
	if(isset($data->harvestsfactory)){
		if($data->harvestsfactory == "1"){
			$bot->SendMsg('=====================================');
			$bot->SendMsg('[Factory] Action harvesting started...');
			$reload = 0;
			foreach ($data->collectFactoryIN as $id){
				$obj = $bot->fobjects["w$id"];
				if(($obj["className"] == "Factory")  && ($obj["state"] == "grown")){
					// $bot->SendMsg('[Factory] Harvesting Factory '.$obj['itemName'].'('.$obj['id'].')');
					$bot->harvestAction($obj, "harvest", "Main Factory");
					$reload++;
					//check for streakbonus
					if(isset($data->Fbonus) && ($data->Fbonus == "1")){
						$bot->streakBonus3();
					}
				}
				
				if(isset($bot->error_msg)){
					$bot->ReloadConfig();
					$hasenergy = $bot->CheckEnergy();
					if(($hasenergy == 0) && ($bot->Energy == 0)){
						break;
					}
				}
			}			
			$bot->SendMsg('[Factory] Action done.');

			if($reload > 0){ $bot->ReloadConfig(); }
		}
	}
}
//==========================================================================
//Harvest Downtown Factory
function HavestDtFactory($bot, $data){
	if(isset($data->harvestsfactory2)){
		if($data->harvestsfactory2 == "1"){
			$bot->SendMsg('=====================================');
			$bot->SendMsg('[Factory] Action harvesting started...');
			$reload = 0;
			foreach ($data->collectFactoryINDt as $id){
				$obj = $bot->dt_fobjects["w$id"];
				if(($obj["className"] == "Factory")  && ($obj["state"] == "grown")){
					// $bot->SendMsg('[Factory] Harvesting Factory '.$obj['itemName'].'('.$obj['id'].')');
					$bot->harvestAction($obj, "harvest", "Downtown Factory");
					$reload++;
					//check for streakbonus
					if(isset($data->Fbonus) && ($data->Fbonus == "1")){
						$bot->streakBonus3();
					}
				}
				
				if(isset($bot->error_msg)){
					$bot->ReloadConfig();
					$hasenergy = $bot->CheckEnergy();
					if(($hasenergy == 0) && ($bot->Energy == 0)){
						break;
					}
				}
			}						
			$bot->SendMsg('[Factory] Action done.');

			if($reload > 0)  { $bot->ReloadConfig(); }
		}
	}
}
//==========================================================================
//Place Contract on Main Factory
function PlaceContractOnMainFactory($bot, $data){
	if(isset($data->placeContract) && isset($data->factorylist) && isset($data->factorylist2)){
		if($data->placeContract == "1"){
			$bot->SendMsg('=====================================');
			$bot->SendMsg('[Factory] Action placing contract started...');
			$reload = 0;
			foreach ($data->collectFactoryIN as $id){
				$obj = $bot->fobjects["w$id"];
				if(($obj["className"] == "Factory")  && ($obj["state"] == "plowed")){
					// $bot->SendMsg('[Factory] Sending Contract to Factory '.$obj['itemName'].'('.$obj['id'].')');
					if(is_int(strpos($obj["itemName"], "textile")) == true){
						$bot->startContract($obj, "Factory", $data->factorylist2, "Main Factory");
					}
					else{
						$bot->startContract($obj, "Factory", $data->factorylist, "Main Factory");
					}
					$reload++;
				}
				if(isset($bot->error_msg)){
					$bot->ReloadConfig();
					$hasenergy = $bot->CheckEnergy();
					if(($hasenergy == 0) && ($bot->Energy == 0)){
						break;
					}
				}
			}		
			$bot->SendMsg('[Factory] Action done.');	

			if($reload > 0){ $bot->ReloadConfig(); }
		}
	}
}
//==========================================================================
//Place Contract on Downtown Factory
function PlaceContractOnDtFactory($bot, $data){
	if(isset($data->placeContract2) && isset($data->factorylist2)){
		if($data->placeContract2 == "1"){
			$bot->SendMsg('=====================================');
			$bot->SendMsg('[Factory] Action placing contract started...');
			$reload = 0;
			foreach ($data->collectFactoryINDt as $id){
				$obj = $bot->dt_fobjects["w$id"];
				if(($obj["className"] == "Factory")  && ($obj["state"] == "plowed")){
					// $bot->SendMsg('[Factory] Sending Contract to Factory '.$obj['itemName'].'('.$obj['id'].')');
					if(is_int(strpos($obj["itemName"], "textile")) == true){
						$bot->startContract($obj, "Factory", $data->factorylist2, "Downtown Factory");
					}
					else{
						$bot->startContract($obj, "Factory", $data->factorylist, "Downtown Factory");
					}
					$reload++;
				}
				if(isset($bot->error_msg)){
					$bot->ReloadConfig();
					$hasenergy = $bot->CheckEnergy();
					if(($hasenergy == 0) && ($bot->Energy == 0)){
						break;
					}
				}
			}
			$bot->SendMsg('[Factory] Action done.');	

			if($reload > 0){ $bot->ReloadConfig(); }			
		}
	}
}
?>